package com.anti.modular.bus.billwaterdetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.billwater.result.BillWaterDetailExcel;
import com.anti.modular.bus.billwaterdetail.entity.BillWaterDetail;
import com.anti.modular.bus.billwaterdetail.enums.BillWaterDetailExceptionEnum;
import com.anti.modular.bus.billwaterdetail.param.BillWaterDetailParam;
import com.anti.modular.bus.billwaterdetail.service.BillWaterDetailService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 账单流水詳情service接口实现类
 *
 * @author anti
 * @date 2021-04-24 13:44:50
 */
@Service
public class BillWaterDetailServiceImpl implements BillWaterDetailService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BillWaterDetail getById(BillWaterDetailParam billWaterDetailParam){
        String sql="select * from bill_water_detail where id =? ";
        return dbUtils.get(sql,BillWaterDetail.class,billWaterDetailParam.getId());
    }

    @Override
    public List<BillWaterDetailExcel> getByIds(List<Long> ids) {
        String sql="select * from bill_water_detail where bill_water_id in (:ids) ";
        HashMap<String, Object> param = CollectionUtil.newHashMap();
        param.put("ids",ids);
        return dbUtils.query(sql,BillWaterDetailExcel.class,param);
    }

    @Override
    public PageResult page(PageParam pageParam,BillWaterDetailParam billWaterDetailParam) {
        StringBuilder sb=new StringBuilder("select * from bill_water_detail where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(billWaterDetailParam)) {
            // 根据費用名稱 查询
            if (ObjectUtil.isNotEmpty(billWaterDetailParam.getName())) {
               sb.append(" and name = ?");
               param.add(billWaterDetailParam.getName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BillWaterDetail> list(BillWaterDetailParam billWaterDetailParam) {
        String sql="";
        return dbUtils.query(sql,BillWaterDetail.class);
    }

    @Override
    public void add(BillWaterDetailParam billWaterDetailParam) {
        BillWaterDetail billWaterDetail = new BillWaterDetail();
        BeanUtil.copyProperties(billWaterDetailParam, billWaterDetail);
        dbUtils.insert(billWaterDetail);
    }

    @Override
    public void add(BillWaterDetail billWaterDetail) {
        dbUtils.insert(billWaterDetail);
    }

    public void delete(BillWaterDetailParam billWaterDetailParam) {
        String sql="delete  from bill_water_detail where id =? ";
        dbUtils.execSql(sql,billWaterDetailParam.getId());
    }

    public void edit(BillWaterDetailParam billWaterDetailParam) {
        BillWaterDetail billWaterDetail = this.queryBillWaterDetail(billWaterDetailParam);
        BeanUtil.copyProperties(billWaterDetailParam, billWaterDetail);
        dbUtils.update(billWaterDetail);
    }

    @Override
    public BillWaterDetail detail(BillWaterDetailParam billWaterDetailParam) {
        return this.queryBillWaterDetail(billWaterDetailParam);
    }

    /**
     * 获取账单流水詳情
     *
     * @author anti
     * @date 2021-04-24 13:44:50
     */
    private BillWaterDetail queryBillWaterDetail(BillWaterDetailParam billWaterDetailParam) {
        BillWaterDetail billWaterDetail = this.getById(billWaterDetailParam);
        if (ObjectUtil.isNull(billWaterDetail)) {
            throw new BusinessException(BillWaterDetailExceptionEnum.NOT_EXIST);
        }
        return billWaterDetail;
    }


}
